package net.lab1024.sa.admin.module.upstream.box.cases;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.enumeration.UpstreamEnum;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartEnumUtil;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * app 设备业务用例组装
 */
@Slf4j
@Component
public class UpstreamServiceUseCase {

    private final Map<String, UpstreamCase> appDeviceUpstreamMap;

    public UpstreamServiceUseCase(Map<String, UpstreamCase> appDeviceUpstreamMap) {
        this.appDeviceUpstreamMap = appDeviceUpstreamMap;
    }

    public UpstreamCase getAppDeviceUpstream(Integer value) {
        UpstreamEnum upstreamEnum = SmartEnumUtil.getEnumByValue(value, UpstreamEnum.class);
        if (upstreamEnum == null) {
            log.error("value:{}，上游值不存在！", value);
            throw new BusinessException("上游异常");
        }
        UpstreamCase upstreamCase = appDeviceUpstreamMap.get(upstreamEnum.getSymbol());
        if (upstreamCase == null) {
            log.error("{}的AppDeviceUpstream实现类不存在！", upstreamEnum.getSymbol());
            throw new BusinessException("上游实现类异常");
        }
        return upstreamCase;
    }
}
